//const {remote: {BrowserWindow}, shell} = require('electron');
const {BrowserWindow, dialog, globalShortcut} = require('@electron/remote')
const {shell, ipcRenderer} = require('electron');
const fs = require("fs");

let newWindows = document.querySelector(".new-windows");

let mainWindows;
newWindows.onclick = function (e) {
    mainWindows = new BrowserWindow({
        width: 400,
        height: 300,
    })
}

let allA = document.querySelectorAll("a");
console.log(allA)
allA.forEach(a => {
    a.onclick = function (e) {
        e.preventDefault();
        shell.openExternal(a.href);
    }
})

let textarea = document.querySelector("textarea");

function openFile() {
    let res = dialog.showOpenDialogSync({
        title: "读取文件",
        buttonLabel: "读取",
        filters: [
            {
                name: "JavaScript",
                extensions: ["js"]
            }
        ]
    });
    console.log(res[0])

    let fileContent = fs.readFileSync(res[0]).toString();

    textarea.value = fileContent;
    console.log()
}

function saveFile() {
    let res = dialog.showSaveDialogSync({
        title: "保存文件",
        buttonLabel: "保存",
        filters: [
            {
                name: "JavaScript",
                extensions: ["js"]
            }
        ]
    });
    fs.writeFileSync(res, textarea.value)
}

globalShortcut.register("CommandOrControl+Y", () => {
    console.log("快捷触发")
})

let windowStart = "min";

function maxWindows(e) {
    console.log(e);
    windowStart = windowStart === "max" ? "min" : "max";
    ipcRenderer.send("max-windows",windowStart);
}

function openConsole() {
    ipcRenderer.send("open-console");
}
